﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;
using System.IO;

namespace TireService.Website.Kunde
{
    public partial class Faktura : System.Web.UI.Page
    {

        #region Public Properties

        public int CustomerId
        {
            get
            {
                if (ViewState["_customerid"] != null)
                    return (int)ViewState["_customerid"];
                else
                    return (int)Session["CustomerId"];
            }
            set
            {
                ViewState["_customerid"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Test propertie
            //this.CustomerId = 10006;

            if (Page.IsPostBack != true)
            {
                BindCustomerInvoice(db, this.CustomerId);
            }
        }

        /// <summary>
        /// Function that bind customer car to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="CustomerId">Id for customer</param>
        private void BindCustomerInvoice(TireServiceDB db, int CustomerId)
        {
            if (Session["InvoiceData"] == null)
            {
                EconomicInterface ecoInterface = new EconomicInterface();

                // Store invoices in session, to avoid API call to economic everytime
                Session["InvoiceData"] = ecoInterface.GetCustomerInvoiceList(CustomerId);
            }

            // Bind to gridview
            //gvInvoice.DataSource = db.GetCustomerInvoices(CustomerId);
            gvInvoice.DataSource = Session["InvoiceData"];
            gvInvoice.DataBind();

            //gvInvoice.ClearSort();
            //gvInvoice.GroupSummary.Clear();
            //gvInvoice.GroupBy(gvInvoice.Columns["CustomerSubscriptionDescription"]);
            //gvInvoice.ExpandAll();
        }

    }
}